package com.example.Project_Module.service.lmpl.impl;

import com.example.Project_Module.dao.RoleDao;
import com.example.Project_Module.dao.UserDao;
import com.example.Project_Module.domain.Role;
import com.example.Project_Module.domain.User;
import com.example.Project_Module.service.UserService;

import java.util.List;

public class UserServiceImpl implements UserService {
    private UserDao userDao;
    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }
    private RoleDao roleDao;

    public void setRoleDao(RoleDao roleDao) {
        this.roleDao = roleDao;
    }

    @Override
    public List<User> list() {
        List<User> userList = userDao.findAll();
        //封装userList每一个roles数据
        for (User user:
             userList) {
            //获得userId
            Long id = user.getId();
            List<Role> roles= roleDao.findRoleByUserId(id);
            user.setRoleList(roles);
        }
        return userList;
    }

    @Override
    public void save(User user, Long[] roleIds) {
        //第一步向sys——user表中存储数据
        Long userId = userDao.save(user);
//        第二步向sys_user_role表中存储数据
        userDao.saveUserRoleRel(userId,roleIds);
    }

    @Override
    public void del(Long userId) {
        //1.删除sys_user_role关系表
        userDao.delUserRoleRel(userId);
        userDao.del(userId);
    }
}
